﻿//@note controller文件夹
var conBaseUrl = '//node//controller' ;

//@note 端口号
var port = 9000 ;

//@note 全局变量定义
global.viewUrl = __dirname + '//views' ;

var express = require('express') ;
var app = express() ;
//@note 静态文件
app.use('/public',express.static(__dirname+'/public')) ;



var Scan = require('Scan') ;
var scan = new Scan({
	url : __dirname + conBaseUrl
}) ;

var appDefConf = {
	method : 'get' ,
	urlSuffix : '.html' ,
	callback : function(req,res){
		res.send('Get request to homepage .') ;
	} 
} ;

var list = scan.start() ;

console.log('=====') ;
for(var i = 0 ; i < list.length ; i++ ){
	
	
	//@note 获取controller
	var controller = require( list[i] ) ;
	var cList = [] ;
	if( typeof(controller) === 'object' ){
		if( controller.length > 0 ){
			cList = controller ;
		}else{
			cList.push(controller) ;
		}
	}
	
	for( var m = 0 ; m < cList.length ; m++ ){
		var defUrl = list[m].replace( scan.getBaseUrl().replace(/\/\//g,'\\') , '' ).replace(/\\/g,'/').replace(/.js$/,appDefConf.urlSuffix) ;
		var shortUrl =  cList[m].url || defUrl ;
	
		app[cList[m].method||appDefConf.method](shortUrl,cList[m].callback || appDefConf.callback) ;
	
		console.log( 'log : ' + shortUrl + ' can be access' ) ;
	}
	
	
	
}
console.log('=====') ;

var server = app.listen(port,function(){
	
	console.log( 'Listening on port %d' , server.address().port ) ;
	
}) ;